package com.xxxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxy.commonResult.CommonResultPage;
import com.xxxy.dao.ExaminationPaperDao;
import com.xxxy.dao.QuestionDao;
import com.xxxy.exception.MyException;
import com.xxxy.model.ExaminationPaper;
import com.xxxy.model.Question;
import com.xxxy.response.ExaminationPaperResponse;
import com.xxxy.response.QuestionListResponse;
import com.xxxy.service.ExaminationPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author sjh
 * @date 2022/2/27 15:57
 */
@Service
public class ExaminationPaperServiceImpl extends ServiceImpl<ExaminationPaperDao, ExaminationPaper> implements ExaminationPaperService {

    @Autowired
    ExaminationPaperDao examinationPaperDao;

    @Autowired
    QuestionDao questionDao;

    @Override
    public CommonResultPage listExamination(Integer page, Integer size, Integer facultyId, Integer courseId, String examinationName, Integer isReviewed, String teacherAccount) {
        PageHelper.startPage(page,size);
        List<ExaminationPaperResponse> examinationPaperList = examinationPaperDao.list(facultyId, courseId, examinationName, isReviewed, teacherAccount);
        PageInfo<ExaminationPaperResponse> pageInfo = new PageInfo<>(examinationPaperList);
        CommonResultPage result = new CommonResultPage();
        result.setTotal(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }

    @Override
    public ExaminationPaperResponse getById(Integer id) {
        ExaminationPaperResponse examinationPaperResponse = examinationPaperDao.getById(id);
        if(examinationPaperResponse != null){
            return examinationPaperResponse;
        }
        throw new MyException("该试卷id未找到");
    }

    @Override
    public List<QuestionListResponse> getQuestionByExaminationPaperId(Integer id) {
        ExaminationPaper examinationPaper = examinationPaperDao.selectById(id);
        if(examinationPaper == null){
            throw new MyException("该试卷id未找到");
        }
        //试题
        String[] questions = examinationPaper.getQuestions().split(",");
        //小节分布
        String[] section = examinationPaper.getSection().split(",");
        //小节分数
        String[] section_score = examinationPaper.getSectionScore().split(",");
        if(questions.length == 0){
            return null;
        }
        List<Question> list = questionDao.listByIdArr(questions);
        List<QuestionListResponse> questionListResponseList = new ArrayList<>();
        //每个小节的题目加入
        int index = 1;
        int section_index = 1;
        for(String i : section){
            //每个小节的最后位置
            int sectionEnd = Integer.parseInt(i);
            QuestionListResponse questionListResponse = new QuestionListResponse();
            List<Question> question = new ArrayList<>();
            while (index <= sectionEnd){
                question.add(list.get(index-1));
                index ++;
            }
            questionListResponse.setSection(section_index);
            questionListResponse.setSection_score(section_score[section_index-1]);
            questionListResponse.setQuestion(question);
            questionListResponseList.add(questionListResponse);
            section_index ++;
        }

        return questionListResponseList;
    }
}
